package com.dreem.factory.absfactory.pizzastore.order;


import com.dreem.factory.absfactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: DesignParrtern
 * @description: 订购披萨
 * @author: Peng
 * @create: 2021-01-25  12:16
 **/
public class OrderPizza {

    private BufferedReader bufferedReader = null;
    private AbsFactory absFactory = null;

    public OrderPizza(AbsFactory absFactory){
        String orderType = null;
        Pizza pizza = null;
        this.absFactory = absFactory;
        do {
            orderType = getType();
            if(orderType.equalsIgnoreCase("exit")){
                closeStream();
                break;
            }
            pizza = absFactory.createPizza(orderType);
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        }while (true);

    }

    // 获取客户希望订购的披萨种类
    private String getType() {
        try{
            bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza 的种类：");
            String str = bufferedReader.readLine();
            return str;
        }catch (Exception e){
            e.printStackTrace();
            return "";
        }
    }
    private void closeStream(){
        if(bufferedReader != null){
            try {bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
